package Exception;

/**
 * @Description:
 * @Author:AMark
 * @Date: 10/17/2023
 */

//通过账户银行类进行模拟
public class InsufficientFundsException extends Exception {
    //账户金额
    private Account account;
    //取款金额
    private double Amount;

    public InsufficientFundsException(Account account, double Amount) {
        this.account = account;
        this.Amount = Amount;
    }

    //覆盖超类方法
    public String getMessage() {
        String str = "账户余额: " + account.getbalance() + "取款额: " + Amount + "余额不足!";
        return str;
    }
}
//账户类
class Account {
    private double balance;

    public void deposite(double Amount) {
        if (Amount > 0.0) {
            balance += Amount;
        }
    }
    public void withdrawal(double Amount) throws InsufficientFundsException {
        //取钱
        if (balance < Amount) {
            throw new InsufficientFundsException(this,Amount);
        }
        balance = balance - Amount;
        System.out.println("取款成功！账户余额为" + balance);
    }
    //获取当前账户余额信息
    public double getbalance() {
        return balance;
    }
}
class test {
    public static void main(String args[]) {
        try {
            Account account = new Account();
            //deposit函数存钱
            account.deposite(1000);
            account.withdrawal(2000);
        } catch (InsufficientFundsException e) {
            //输出先前定义的输出信息
            System.out.println(e.getMessage());
        }
    }
}

